/**
 * 
 */
package com.ysten.basic.common.log;

import java.lang.reflect.Method;
import java.util.Arrays;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @author chenbo
 *
 */
@Aspect
@Component
public class LoggerAspect
{
    
    @Pointcut("@within(org.springframework.stereotype.Controller) || @within(org.springframework.web.bind.annotation.ControllerAdvice)")
    public void controllerLayer()
    {
    }
    
    @Pointcut("@annotation(com.ysten.basic.common.log.AopLog)")
    public void logLayer()
    {
    }
    
    @Around("controllerLayer() || logLayer()")
    public Object logger(ProceedingJoinPoint jp)
        throws Throwable
    {
        org.slf4j.Logger log = LoggerFactory.getLogger(jp.getTarget().getClass());
        Object o;
        if (log.isDebugEnabled())
        {
            long current = System.currentTimeMillis();
            MethodSignature methodjp = (MethodSignature)jp.getSignature();
            Method method = methodjp.getMethod();
            log.debug("Enter func[{}]. input parameters：{}", method.getName(), Arrays.toString(jp.getArgs()));
            o = jp.proceed();
            log.debug("Exit Func[{}]. return value: {},cost:{}ms",
                method.getName(),
                o,
                (System.currentTimeMillis() - current));
        }
        else
        {
            o = jp.proceed();
        }
        return o;
    }
    
}
